<?php
	namespace app\index\controller;

	use \think\Db;

	use \think\Session;

	class Login extends Base{

		public function index(){

			try{

				if($this->method=='post'){

					$user=input('post.user');

					if($user==''){

						return $this->fail('请输入用户名~');

					}

					$pwd=input('post.pwd');

					if($pwd==''){

						return $this->fail('请输入密码~');

					}

					$row=Db::name('user')->where(['u_user'=>$user])->find();

					if($row && $row['u_pwd']==md5($pwd)){

						Session::set('userInfo',['id'=>$row['u_id'],'user'=>$row['u_user']]);

						return $this->success();

					}

					return $this->fail('账号或密码不正确~');


				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function check_login(){

			try{

				if($this->method=='post'){

					$user=Session::get('userInfo');

					$type=(int)input('post.type');

					if($user){

						if($type==1){

							$data=Db::name('user')->field('u_user,u_icon,u_money')->where(['u_id'=>$user['id']])->find();

						}else{
							$data=['user'=>$user['user']];
						}

						return $this->success($data);
					}

					return $this->fail('',-1);

				}else{

					echo "拒绝访问";

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function exit(){

			try{

				if($this->method=='post'){

					Session::set('userInfo',null);

					return $this->success();

				}else{
					echo "拒绝访问";
				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

	}

?>